/**@FILE:OrderServiceImpl.java
 * @AUTHOR:qingsheng.zheng
 * @DATE:2015-1-31 下午6:31:57*/
package com.xf.toy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.xf.pub.exception.ServiceException;
import com.xf.toy.bean.Toyorder;
import com.xf.toy.bean.ToyorderExample;
import com.xf.toy.bean.ToyorderExample.Criteria;
import com.xf.toy.bean.User;
import com.xf.toy.bean.Wish;
import com.xf.toy.dao.ToyorderMapper;
import com.xf.toy.dao.page.Pagination;
import com.xf.toy.service.OrderService;
import com.xf.toy.service.ToyService;
import com.xf.toy.service.UserService;
import com.xf.toy.service.WishService;
import com.xf.toy.service.constant.ToyConst;

/*******************************************
 * @COMPANY:alibaba
 * @CLASS:OrderServiceImpl
 * @DESCRIPTION:
 * @AUTHOR:qingsheng.zheng
 * @VERSION:v1.0
 * @DATE:2015-1-31 下午6:31:57
 *******************************************/
@Service("orderService")
public class OrderServiceImpl implements OrderService {
	@Resource
	private ToyorderMapper orderMapper;
	@Resource(name = "wishService")
	private WishService wishService;
	@Resource(name = "userService")
	private UserService userService;
	@Resource(name = "toyService")
	private ToyService toyService;

	@Override
	public Toyorder selectOrderDetail(Long orderid) {
		return orderMapper.selectByPrimaryKey(orderid);
	}

	@Override
	public List<Toyorder> selectOrderList(Long userid,String toycode, Long orderid,
			List<String> orderStatus, Pagination page) {
		ToyorderExample orderExample = new ToyorderExample();
		if (page != null) {
			orderExample.setLimitStart(page.getStart());
			orderExample.setLimitEnd(page.getPageSize());
		}
		Criteria criteria = orderExample.createCriteria();
		if (userid != null) {
			criteria.andUserIdEqualTo(userid);
		}
		if (orderid != null) {
			criteria.andOrderIdEqualTo(orderid);
		}
		if(!StringUtils.isBlank(toycode))
			criteria.andToyCodeEqualTo(toycode);
		criteria.andStatusIn(orderStatus);
		page.setTotalcount(orderMapper.countByExample(orderExample));
		return orderMapper.selectByExample(orderExample);

	}

	public int selectCounyByStatus(Long uid,List<String> orderStatus) {
		ToyorderExample orderExample = new ToyorderExample();
		Criteria criteria = orderExample.createCriteria();
		criteria.andStatusIn(orderStatus);
		criteria.andUserIdEqualTo(uid);
		return orderMapper.countByExample(orderExample);
	}

	@Override
	public void createOrder(Toyorder order) {
		orderMapper.insert(order);
	}

	@Override
	public void createOrderByWishId(Long wishId) {
		Wish wish = wishService.selectWishByWishId(wishId);
		User user = userService.selectUserDetail(wish.getUserId());
		Toyorder temp = this.selectOrderByWishId(wishId);
		if (temp != null)
			throw new ServiceException("已经创建订单!");
		//List<String> statuss = new ArrayList<String>();
		//statuss.add(ToyConst.TOY_ORDER_STATUS_DELIVER);
		//statuss.add(ToyConst.TOY_ORDER_STATUS_WAIT);
		//int count = this.selectCounyByStatus(user.getUserId(),statuss);
		//if (count > 1||count<=1)
		//	throw new ServiceException(20000l);
		Toyorder order = new Toyorder();
		order.setAddress(user.getAddress());
		order.setConfirmTime(new Date());
		order.setMobile(user.getMobile());
		order.setReturntime(null);
		order.setStatus(ToyConst.TOY_ORDER_STATUS_WAIT);
		order.setToyCode(wish.getToyCode());
		order.setUserId(wish.getUserId());
		order.setWishId(wishId);
		this.createOrder(order);
		toyService.toyBack(wish.getToyCode(), -1L);
		wish.setOrderId(order.getOrderId());
		wish.setWishId(order.getWishId());
		wish.setStatus(ToyConst.TOY_WISH_STATUS_ORDER);
		wishService.updateWish(wish);

	}

	@Override
	public void updateOrder(Toyorder order) {
		orderMapper.updateByPrimaryKey(order);

	}

	@Override
	public void updateOrderStatus(Long orderid, String status) {
		Toyorder order = new Toyorder();
		order.setOrderId(orderid);
		order.setStatus(status);
		this.orderMapper.updateByPrimaryKeySelective(order);

	}

	@Override
	public Toyorder selectOrderByWishId(Long wishId) {
		ToyorderExample orderExample = new ToyorderExample();
		Criteria criteria = orderExample.createCriteria();
		criteria.andWishIdEqualTo(wishId);
		List<Toyorder> orders = orderMapper.selectByExample(orderExample);
		if (orders == null || orders.isEmpty() || orders.size() > 1)
			return null;
		return orders.get(0);

	}

	@Override
	public int selectCount(Toyorder order) {
		return 0;
	}

}
